package org.spring.demo.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;

/**
 * @author LHB
 * @date 2019/12/19 12:52
 */
@Configuration
public class ActivemqConfig {
    @Value("spring.jms.pub-sub-domain")
    private boolean pubSubDomain;

    /**
     * mq链接工厂设置
     *
     * @param brokerUrl
     * @param username
     * @param password
     * @return
     */

    @Primary
    @Bean(name = "testConnectionFactory")
    public ActiveMQConnectionFactory testConnectionFactory(
            @Value("${spring.inner.activemq.brokerUrl}") String brokerUrl,
            @Value("${spring.inner.activemq.user}") String username,
            @Value("${spring.inner.activemq.password}") String password) {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setBrokerURL(brokerUrl);
        activeMQConnectionFactory.setUserName(username);
        activeMQConnectionFactory.setPassword(password);

        return activeMQConnectionFactory;
    }

    /**
     * 监听配置
     *
     * @param connectionFactory
     * @return
     */
    @Bean(name = "listenerFactory")
    @Primary
    public JmsListenerContainerFactory activeConnectionFactory(
            @Qualifier("testConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(pubSubDomain);
        return factory;
    }

    /**
     * mq发送配置
     *
     * @param connectionFactory
     * @return
     */
    //@ConditionalOnProperty(name = "spring.inner.activemq.enabled", havingValue = "true")
    @Bean(name = "activeTest1")
    @Primary
    public JmsTemplate innerActivemqTemplate(
            @Qualifier("testConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
        JmsTemplate template = new JmsTemplate(connectionFactory);
        template.setPubSubDomain(pubSubDomain);
        return template;
    }

//topic ↑
//###############################################################################################################
//queue ↓
    @Bean(name = "testConnectionFactory2")
    public ActiveMQConnectionFactory activeConnectionFactory2(
            @Value("${spring.two.activemq.brokerUrl}") String brokerUrl,
            @Value("${spring.two.activemq.user}") String username,
            @Value("${spring.two.activemq.password}") String password) {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setBrokerURL(brokerUrl);
        activeMQConnectionFactory.setUserName(username);
        activeMQConnectionFactory.setPassword(password);
        return activeMQConnectionFactory;
    }


    @Bean(name = "activeTest2")
    public JmsTemplate innerActivemqTemplate2(
            @Qualifier("testConnectionFactory2") ActiveMQConnectionFactory connectionFactory) {
        JmsTemplate template = new JmsTemplate(connectionFactory);
        return template;
    }

    /**
     * 监听配置
     *
     * @param connectionFactory
     * @return
     */
    @Bean(name = "listenerFactory2")
    public JmsListenerContainerFactory listenerFactory2(
            @Qualifier("testConnectionFactory2") ActiveMQConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMaxMessagesPerTask(10);
        return factory;
    }


}
